package com.zhudi.develop_demo_as.network;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhudi.develop.util.ZhudiViewUtil;
import com.zhudi.develop_demo_as.R;

/**
 * Created by zhudi on 2015/9/14.
 * 遮罩
 */
public class LoadingLayer {
    private static int showNum = 0;
    private WindowManager wmManager ;
    private WindowManager.LayoutParams wmParams;
    private Context mContext;

    private static LoadingLayer instance;
    private LoadingView loadingView;

    /**
     * 显示
     */
    public synchronized static void show(){
        LoadingLayer loadingLayer = getInstance();
//        try {
            LoadingLayer.showNum ++;
            loadingLayer.showLoadingView();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    /**
     * 隐藏
     */
    public synchronized static void hide(){
        LoadingLayer loadingLayer = getInstance();
        LoadingLayer.showNum--;
        if(LoadingLayer.showNum == 0){
            loadingLayer.removeLoadingView();
        }
    }

    public static void setMiddleText(String text){
        getInstance().loadingView.middleTextView.setText(text);
    }

    public static void setBottomText(String text){
        getInstance().loadingView.bottomTextView.setVisibility(View.VISIBLE);
        getInstance().loadingView.bottomTextView.setText(text);
    }

    public static void setEnableRemoved(boolean b){
        getInstance().loadingView.enableRemoved = b;
    }

    public void tryHide(){
        removeLoadingView();
    }

    public static void init(Context context){
        if(instance != null){
            instance.tryHide();
        }
        instance = new LoadingLayer(context);
    }

    public static LoadingLayer getInstance(){
        return instance;
    }

    private LoadingLayer(Context context){
        mContext = context;
        wmManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wmParams = new WindowManager.LayoutParams();
        wmParams.width = -1;
        wmParams.height = -1;
        wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
        wmParams.format = PixelFormat.TRANSLUCENT;

        wmParams.flags = LayoutParams.FLAG_LAYOUT_INSET_DECOR;
        wmParams.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED;
    }

    private void showLoadingView(){
        if(loadingView != null){
            return ;
        }
        loadingView = new LoadingView(mContext);
        wmManager.addView(loadingView, wmParams);
    }

    private void removeLoadingView(){
        if(loadingView == null){
            return ;
        }
        try {
            wmManager.removeViewImmediate(loadingView);
        } catch (Exception e) {
        }
        loadingView = null;
    }


    public class LoadingView extends RelativeLayout {

        private TextView middleTextView;
        private TextView bottomTextView;

        private boolean enableRemoved = true;

        public LoadingView(Context context) {
            super(context);
//			setBackgroundColor(0x88000000);
            int size = 120;

            int sizeLayout =  ZhudiViewUtil.dip2px(context, size - 40);
            LayoutParams layoutParams = new RelativeLayout.LayoutParams(sizeLayout, sizeLayout);
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

            RelativeLayout backView = new RelativeLayout(context);
            backView.setBackgroundResource(R.drawable.loading_bg);
            //int backSize = (int) (size * 1.8);
            int backSize = ZhudiViewUtil.dip2px(context, size);
            LayoutParams lpBack = new RelativeLayout.LayoutParams(backSize, backSize);
            lpBack.addRule(RelativeLayout.CENTER_IN_PARENT);
            middleTextView = new TextView(context);
            middleTextView.setTextSize(12);
            middleTextView.setTextColor(getResources().getColor(R.color.white));
            middleTextView.setGravity(Gravity.CENTER);
            middleTextView.setText("Loading...");
            //middleTextView.setId(12345);

            bottomTextView = new TextView(context);
            bottomTextView.setTextSize(12);
            bottomTextView.setTextColor(getResources().getColor(R.color.white));
            bottomTextView.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
            bottomTextView.setPadding(0, 0, 0, 15);
            bottomTextView.setText("Loading...");


            ImageView imageView = new ImageView(context);
            imageView.setImageResource(R.drawable.u_loading_2);
            //int imageSize = ZhudiViewUtil.dip2px(context, sizeLoadingImage);
            //ZhudiScreenUtil.setWidthHeightNumber(imageView,imageSize,imageSize);
            //imageView.setId(1);

            this.addView(backView, lpBack);
            this.addView(imageView,layoutParams);
            this.addView(middleTextView,layoutParams);

            LayoutParams lpBottom = new RelativeLayout.LayoutParams(backSize, backSize);
            lpBottom.addRule(RelativeLayout.CENTER_IN_PARENT);
            backView.addView(bottomTextView, lpBottom);
            bottomTextView.setVisibility(View.GONE);
            Animation loadingAnim = AnimationUtils.loadAnimation(context, R.anim.loading);
            imageView.startAnimation(loadingAnim);
        }

        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if(event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP){
                if(enableRemoved){
                    removeLoadingView();
                }
                return true;
            }
            return super.dispatchKeyEvent(event);
        }
    }
}
